<?php
defined('_XR_EXEC') or die();
/**
 * 逻辑处理类 
 */
class report_trans_Model extends report_base_model
{

	/* 会员账户收支汇总报表 */
	public function trans1($formdata){
		$db = factory::getdbo();
		
		if(!isset($formdata['b_date']))    $formdata['b_date']  = ''; 
		if(!isset($formdata['e_date']))    $formdata['e_date']  = '';  
		if(!isset($formdata['state']))    	 $formdata['state']  = ''; 
		if(!isset($formdata['classes']))    	 $formdata['classes']  = '';  
		if(!isset($formdata['sendtype']))    	 $formdata['sendtype']  = '';   
		
		$sql_where = 'select 
		a.id ,
		a.username,
		a.name,
		a.contractmoney,
		a.sendtype,
		a.create_date,
		a.balance
		from t_user a  
		where a.usertype=2 ';
		
		if($formdata['b_date']!=''){
			$sql_where .= ' and a.create_date >= "'.$formdata['b_date'].'"';	
		}
		if($formdata['e_date']!=''){
			$sql_where .= ' and a.create_date <= "'.$formdata['e_date'].'"';	
		} 
		if($formdata['classes']!=''){
			$sql_where .= ' and a.classes = "'.$formdata['classes'].'"';	
		}
		if($formdata['state']!=''){
			$sql_where .= ' and a.state = "'.$formdata['state'].'"';	
		} 
		if($formdata['sendtype']!=''){
			$sql_where .= ' and a.sendtype = "'.$formdata['sendtype'].'"';	
		} 
		
		$rs = $db->loadassoclist($sql_where);
		 
		$s = '
		<table border="1">
			<thead> 
				<tr>
					<th>序号</th>
					<th>会员姓名</th> 
					<th>会员编号</th>
					<th>会员创建日期</th> 
					<th>配送类别</th> 
					<th>合同金额</th>
					<th>充值总金额</th>
					<th>消费总金额</th>
					<th>当前余额</th>
					<th>备注</th>
				</tr>
			</thead>
			<tbody>
		';
		$totalval = array(
			'allpay'=>0,
			'alluse'=>0,
			'balance'=>0,
			'contractmoney'=>0
		);
		foreach($rs as $k=>$v){
			$v['allpay'] = abs($db->loadresult('select sum(a.fee) from t_trans a left join t_transtype b on a.transtype_id=b.id where a.user_id="'.$v['id'].'" and b.actions="+" '));
			$v['alluse'] = abs($db->loadresult('select sum(a.fee) from t_trans a left join t_transtype b on a.transtype_id=b.id where a.user_id="'.$v['id'].'" and b.actions="-" '));
			
			
			$totalval['allpay'] += $v['allpay'];
			$totalval['alluse'] += $v['alluse'];
			$totalval['balance'] += $v['balance'];
			$totalval['contractmoney'] += doubleval($v['contractmoney']);
			$s .= '
				<tr>
					<td>'.($k+1).'</td>
					<td>'.$v['name'].'</td>
					<td>'.$v['username'].'</td>
					<td>'.$v['create_date'].'</td>
					<td>'.($v['sendtype']=='0'?"乐园卡":"田园卡").'</td>
					<td style="text-align:right">'.format::dec($v['contractmoney'],2).'</td>
					<td style="text-align:right">'.format::dec($v['allpay'],2).'</td>
					<td style="text-align:right">'.format::dec($v['alluse'],2).'</td>
					<td style="text-align:right">'.format::dec($v['balance'],2).'</td>
					<td> </td>
				</tr>
			';
		} 
		$s .= '
				<tr>
					<td colspan="5"  style="text-align:right">总计</td> 
					<td style="text-align:right">'.format::dec($totalval['contractmoney'],2).'</td>
					<td style="text-align:right">'.format::dec($totalval['allpay'],2).'</td>
					<td style="text-align:right">'.format::dec($totalval['alluse'],2).'</td>
					<td style="text-align:right">'.format::dec($totalval['balance'],2).'</td>
					<td> </td>
				</tr>
			</tbody>
		</table>
		';
		return $s; 

	}
	/* 会员账户收支汇总报表 */
	public function trans2($formdata){
		$db = factory::getdbo();
		 /* ---------------------debug begin---------------------

Array
(
    [b_date] => 
    [e_date] => 
    [transtype] => 1,2,3,4,5
    [ids] => 
)
 */
		if(!isset($formdata['b_date']))    $formdata['b_date']  = ''; 
		if(!isset($formdata['e_date']))    $formdata['e_date']  = '';  
		if(!isset($formdata['transtype']))    	 $formdata['transtype']  = ''; 
		if(!isset($formdata['ids']))    	 $formdata['ids']  = '';  
		
		$sql_where = 'select 
		a.id,
		a.fee,
		a.balance,
		a.remark,
		a.created,
		b.actions,
		b.name as transtype_name,
		c.username,
		c.name as user_name
		from t_trans a  
		left join t_transtype b on a.transtype_id=b.id
		left join t_user c on a.user_id=c.id
		where a.transtype_id in ('.$formdata['transtype'].') ';
		
		if($formdata['b_date']!=''){
			$sql_where .= ' and a.created >= "'.$formdata['b_date'].'"';	
		}
		if($formdata['e_date']!=''){
			$sql_where .= ' and a.created <= "'.$formdata['e_date'].'"';	
		}
		
		if($formdata['ids']!=''){
			$sql_where .= ' and a.user_id in ('.$formdata['ids'].')';	
		} 
		$rs = $db->loadassoclist($sql_where); 
		$s = '
		<table border="1">
			<thead> 
				<tr>
					<th>序号</th>
					<th>会员姓名</th> 
					<th>会员编号</th>
					<th>消费类别</th> 
					<th>支出金额</th> 
					<th>存入金额</th>
					<th>变动后金额</th>
					<th>日期</th>
					<th>备注</th>
				</tr>
			</thead>
			<tbody>
		';
		foreach($rs as $k=>$v){
			$s .= '
				<tr>
					<td>'.($k+1).'</td>
					<td>'.$v['user_name'].'</td>
					<td>'.$v['username'].'</td>
					<td>'.$v['transtype_name'].'</td>
					<td>'.($v['actions']=='-'?format::dec(abs($v['fee']),2):"").'</td>
					<td>'.($v['actions']=='+'?format::dec(abs($v['fee']),2):"").'</td>
					<td style="text-align:right">'.format::dec($v['balance'],2).'</td>
					<td>'.$v['created'].'</td>
					<td>'.$v['remark'].'</td>
				</tr>
			';
		}
		return $s; 
		
	}	
	
}

?>